﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms;
using System.Drawing;

namespace hf_plat_terminal
{
	/// <summary>
	/// 
	/// </summary>
	public class ChartDB : Chart
	{
		/// <summary>
		/// 
		/// </summary>
		public ChartDB()
		{
			SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
			UpdateStyles();

			this.AxisViewChanged += new EventHandler<ViewEventArgs>(ChartDB_AxisViewChanged);
		}

		void ChartDB_AxisViewChanged(object sender, ViewEventArgs e)
		{
			ResetAxisY();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnHandleCreated(EventArgs e)
		{
			base.OnHandleCreated(e);
			if (this.BackColor != Color.Transparent)
			{
				this.BackColor = Color.Transparent;
				this.Series[0].Name = "0";

				this.ChartAreas[0].BackColor = Color.WhiteSmoke;
				this.ChartAreas[0].AxisX.IsStartedFromZero = false;
				this.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
				this.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightCyan;
				this.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
				this.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;

				this.ChartAreas[0].AxisY.IsStartedFromZero = false;
				this.ChartAreas[0].AxisY.IntervalAutoMode = IntervalAutoMode.VariableCount;
				this.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.DarkRed;
				this.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
				this.ChartAreas[0].AxisY.MajorTickMark.Enabled = true;
				this.ChartAreas[0].AxisY.ScaleView.Zoomable = false;		//Y轴不缩放
				this.ChartAreas[0].AxisY.ScrollBar.Enabled = false;		//Y轴不显示滚动条

				this.ChartAreas[0].AxisY.IsInterlaced = true;	//交错显示

				this.ChartAreas[0].BorderColor = Color.Maroon;
				this.ChartAreas[0].BorderWidth = 1;
				this.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;

				this.Legends.Clear();

				this.Series[0].IsXValueIndexed = true;
				this.Series[0].XValueType = ChartValueType.Date;

				this.Series[0].ChartType = SeriesChartType.Line;
				this.Series[0].Color = Color.Red;
				this.Series[0].MarkerStyle = MarkerStyle.Circle;
				this.Series[0].MarkerColor = Color.White;
				this.Series[0].MarkerBorderColor = Color.Red;

				this.Series[0].ToolTip = "#VALX #VAL{N2}";
				this.Series[0].LabelToolTip = "#VALX #VAL{N2}";
			}
		}

		internal void ResetAxisY()
		{
			if (this.ChartAreas[0].AxisX.ScaleView.IsZoomed)
			{
				Series sCur = this.Series[0];
				int left = Math.Max(0, (int)this.ChartAreas[0].AxisX.ScaleView.ViewMinimum);
				int right = Math.Min(sCur.Points.Count - 1, (int)this.ChartAreas[0].AxisX.ScaleView.ViewMaximum);

				double viewTop = double.MinValue, viewButtom = double.MaxValue;

				////调整纵坐标
				//for (int i = left; i <= right; i++)
				//{
				//    viewTop = Math.Max(viewTop, this.Series[0].Points[i].YValues[0]);
				//    viewButtom = Math.Min(viewButtom, this.Series[0].Points[i].YValues[this.Series[0].YValuesPerPoint > 1 ? 1 : 0]);
				//}
				////viewTop += 10 * YValueTick;
				////viewButtom -= 10 * YValueTick;
				//viewTop *= 1.01;
				//viewButtom *= 0.99;
				//this.ChartAreas[0].AxisY.ScaleView.Zoom(viewButtom, viewTop);

				for (int i = 0; i < this.ChartAreas.Count; i++)
				{
					viewTop = double.MinValue;
					viewButtom = double.MaxValue;
					if (this.ChartAreas[i].Visible)
					{
						foreach (Series s in this.Series.Where(n => n.ChartArea == this.ChartAreas[i].Name))
						{
							for (int j = left; j <= right; j++)
							{
								viewTop = Math.Max(viewTop, s.Points[j].YValues[0]);
								viewButtom = Math.Min(viewButtom, s.Points[j].YValues[s.YValuesPerPoint > 1 ? 1 : 0]);
							}
						}
						//viewTop += 10 * YValueTick;
						//viewButtom -= 10 * YValueTick;
						//viewTop = (Math.Ceiling(viewTop / 100) + 1) * 100;// *= 1.01;
						//viewButtom = viewButtom / 100 * 100;// *= 0.99;
						//viewTop = viewTop / this.YValueTick * this.YValueTick;
						//viewButtom = viewButtom / this.YValueTick * this.YValueTick;
						double baseY = Math.Pow(10, Math.Max(0, Math.Ceiling(viewTop).ToString().Length - 3));
						if (baseY == 1)
						{
							viewTop = Math.Ceiling(viewTop);
							viewButtom = Math.Floor(viewButtom);
						}
						else
						{
							viewTop = Math.Ceiling(viewTop / baseY) * baseY + baseY;
							viewButtom = Math.Floor(viewButtom / baseY) * baseY - baseY;
						}
						this.ChartAreas[i].AxisY.ScaleView.Zoom(viewButtom, viewTop);
					}
				}
			}
		}

		/// <summary>
		/// Y值最小变动
		/// </summary>
		public double YValueTick { get; set; }

		/// <summary>
		/// 缩放
		/// </summary>
		public void Zoom()
		{
			if (this.Series[0].Points.Count > 200)
			{
				this.ChartAreas[0].AxisX.ScaleView.Zoom(this.Series[0].Points.Count - 150, this.Series[0].Points.Count);
				this.ResetAxisY();
			}
			else
			{
				this.ChartAreas[0].AxisX.ScaleView.ZoomReset();
				this.ChartAreas[0].AxisY.ScaleView.ZoomReset();
			}
		}

		/// <summary>
		/// 放大
		/// </summary>
		public void ZoomOut()
		{
			if (!this.ChartAreas[0].AxisX.ScaleView.IsZoomed)
				this.Zoom();

			Series sCur = this.Series[0];
			int left = (int)this.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
			int right = (int)this.ChartAreas[0].AxisX.ScaleView.ViewMaximum;
			if (right - left > 20)
			{
				this.ChartAreas[0].AxisX.ScaleView.Zoom(left + (right - left) / 3, right);
				this.ResetAxisY();
			}
		}

		/// <summary>
		/// 缩小
		/// </summary>
		public void ZoomIn()
		{
			if (this.ChartAreas[0].AxisX.ScaleView.IsZoomed)
			{
				Series sCur = this.Series[0];
				int left = (int)this.ChartAreas[0].AxisX.ScaleView.ViewMinimum;
				int right = (int)this.ChartAreas[0].AxisX.ScaleView.ViewMaximum;
				if (left <= 1 && right >= sCur.Points.Count)
				{
					this.ChartAreas[0].AxisX.ScaleView.ZoomReset();
					this.ChartAreas[0].AxisY.ScaleView.ZoomReset();
				}
				else
				{
					if (left == 0)
						this.ChartAreas[0].AxisX.ScaleView.Zoom(left, right + (right - left) / 3);
					else
						this.ChartAreas[0].AxisX.ScaleView.Zoom(left - (right - left) / 3, right);
					this.ResetAxisY();
				}
			}
		}
	}
}
